package com.sky.myapplication.kotlin.kotlinActivity

import android.Manifest
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import com.sky.myapplication.R
import java.io.*


/**
 * @Author: Minxp
 * @UpdateDate: 2021/7/8 0008
 * @Description: java类作用描述
 */
class FileSteamKotlinActivity: AppCompatActivity() {
    lateinit var et_input: EditText
    lateinit var tv_text: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_file_steam)

//        handlePermisson()
        initView()
    }

    fun initView() {
        var btn_inner_file: Button = findViewById(R.id.btn_inner_file)
        var btn_out_file: Button = findViewById(R.id.btn_out_file)

        et_input = findViewById(R.id.et_input)
        tv_text = findViewById(R.id.tv_text)

        btn_inner_file.setOnClickListener {
            innerFile()
        }

        btn_out_file.setOnClickListener {
            outFile()
        }
    }

    fun innerFile() {
        //android平台中能操作文件夹只有两个地方：1.sdcard 2.data/data/<package-name>/files



//        val outData: String = et_input.text.toString()
//        val filename = "/data/com.sky.myapplication/"
//
//        var file = File(filename)
//        if(!file.exists()){
//            file.mkdirs();
//        }

        val text = et_input.text.toString()

        val a = getDir("sky", MODE_PRIVATE)
        val local_file: String = a.getAbsolutePath().toString() + "/" + "cache.txt"
        val f = File(local_file)

        try {
            if (!f.exists()) {
                f.createNewFile()
            }
            f.appendText(text)
        } catch (ex: Exception) {
            println(ex)
        }


//        var fos: FileOutputStream? = null
//        try {
//            fos = openFileOutput(local_file, MODE_PRIVATE)
//            fos.write(text.toByteArray())
//        } catch (e: FileNotFoundException) {
//            e.printStackTrace()
//        } catch (e: IOException) {
//            e.printStackTrace()
//        } finally { //关闭文件流
//            if (fos != null) {
//                try {
//                    fos.close()
//                } catch (e: IOException) {
//                    e.printStackTrace()
//                }
//            }
//        }

//        val outData: String = et_input.text.toString() //需要写入内部存储的数据
//        var file = File("data/data/com.sky.myapplication/innerFile.txt")
//        if(!file.exists()){
//            file.createNewFile()
//        }
//        file.writeText(outData)


    }

    fun outFile() {
        val a = getDir("sky", MODE_PRIVATE)
        val local_file: String = a.getAbsolutePath().toString() + "/" + "cache.txt"
        val f = File(local_file)

        var text = f.readText()
        tv_text.text = text

//        val filename = "innerFile"
//        var fis: FileInputStream? = null
//        var text: String? = null
//        try {
//            fis = openFileInput(filename)
//            var bf = BufferedReader(InputStreamReader(fis))
//            while (bf.readLine() != null) {
//                text = bf.readLine()
//            }
//        }  catch (e: FileNotFoundException) {
//            e.printStackTrace()
//        } catch (e: IOException) {
//            e.printStackTrace()
//        } finally { //关闭文件流
//            if (fis != null) {
//                try {
//                    fis.close()
//                } catch (e: IOException) {
//                    e.printStackTrace()
//                }
//            }
//        }

//        val filename = "innerFile"
//        var text = File(filename).readText()
//        tv_text.text = text
    }

    fun handlePermisson(){

        // 需要动态申请的权限
        val permission = Manifest.permission.WRITE_EXTERNAL_STORAGE

        //查看是否已有权限
        val checkSelfPermission = ActivityCompat.checkSelfPermission(this,permission)

        if (checkSelfPermission != PackageManager.PERMISSION_GRANTED) {
            //没有拿到权限  是否需要在第二次请求权限的情况下
            // 先自定义弹框说明 同意后在请求系统权限(就是是否需要自定义DialogActivity)
            myRequestPermission()
        }
    }

    private fun myRequestPermission() {
        //可以添加多个权限申请
        val permissions = arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(permissions,1)
        }
    }

    /***
     * 权限请求结果  在Activity 重新这个方法 得到获取权限的结果  可以返回多个结果
     */
    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        //是否获取到权限
        if(grantResults[0] == PackageManager.PERMISSION_GRANTED){

        }
    }

}